#!/bin/sh

#
# Test resources in subdirectories
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
file-resource subdir/onesubdir {
	host-path = "${TESTFILE_1K}"
}
file-resource /rootfile {
	host-path = "${TESTFILE_1K}"
}
file-resource subdir1/subdir2/twosubdir {
        host-path = "${TESTFILE_1K}"
}

task complete {
        on-resource subdir/onesubdir { raw_write(0) }
        on-resource /rootfile { raw_write(4) }
        on-resource subdir1/subdir2/twosubdir { raw_write(8) }
}
EOF

cat >$EXPECTED_META_CONF <<EOF
file-resource "subdir/onesubdir" {
  length=1024
  blake2b-256="b25c2dfe31707f5572d9a3670d0dcfe5d59ccb010e6aba3b81aad133eb5e378b"
}
file-resource "/rootfile" {
  length=1024
  blake2b-256="b25c2dfe31707f5572d9a3670d0dcfe5d59ccb010e6aba3b81aad133eb5e378b"
}
file-resource "subdir1/subdir2/twosubdir" {
  length=1024
  blake2b-256="b25c2dfe31707f5572d9a3670d0dcfe5d59ccb010e6aba3b81aad133eb5e378b"
}
task "complete" {
  on-resource "subdir/onesubdir" {
    funlist = {"2", "raw_write", "0"}
  }
  on-resource "/rootfile" {
    funlist = {"2", "raw_write", "4"}
  }
  on-resource "subdir1/subdir2/twosubdir" {
    funlist = {"2", "raw_write", "8"}
  }
}
EOF

$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Check that the zip file was created as expected
check_meta_conf
cmp $TESTFILE_1K $UNZIPDIR/data/subdir/onesubdir
cmp $TESTFILE_1K $UNZIPDIR/rootfile
cmp $TESTFILE_1K $UNZIPDIR/data/subdir1/subdir2/twosubdir

# Check that applying the firmware does the expected thing
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete
dd if=$TESTFILE_1K seek=0 of=$WORK/expected.bin
dd if=$TESTFILE_1K seek=4 of=$WORK/expected.bin
dd if=$TESTFILE_1K seek=8 of=$WORK/expected.bin
cmp_bytes 5120 $IMGFILE $WORK/expected.bin

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
